package com.flight.trans.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.LeavePorWeighingDetailsAdapter;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.ChannelBean.ChannelData;
import com.flight.trans.bean.ErrorRouteBean;
import com.flight.trans.bean.LeavePorWeighingDetailsBean.LeavePorWeighingDetailsData;
import com.flight.trans.bean.LeavePorWeighingDetailsBean.LeavePorWeighingDetailsData.OrderBean;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.dialog.ChanelDialog;
import com.flight.trans.view.edit.EditorActivity;
import com.lzy.okgo.model.HttpParams;

import java.util.List;

/**
 * 出港称重
 */
public class LeavePortWeighingDetailsActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mLlContainer ;
    private TextView mTvTitleName;
    private RecyclerView mRvContainer ;

    private Button mBtnSubmit ,//提交
            mBtnRevise;//修改

    private TextView mTvWeighbridgeRoute ,//选取通道
            mTvContainerNo ,//容器编号
            mTvFlightNo ,//航班号
            mTvFlightDate,//航班日期
            mTvWeightingWeight ,//称重重量
            mTvOrderWeight ,//货邮重量
            mTvContainerWeight ,//容器重量
            mTvTotalWeight ;//总重量
    private ImageButton mIbBridgeWeigh ;//获取重量

    private LeavePorWeighingDetailsAdapter mWeighingAdapter;

    private LeavePorWeighingDetailsData mLeavePorWeighingDetailsData ;
    private List<OrderBean> mOrderList;

    private String mContainerCode ;
    private int mContainerType ,ID;

    private ChannelData mChannelData ;
    private List<ChannelData> mChannelDataList ;

    @Override
    protected int setContentView() {
        return R.layout.activity_leave_port_weighing_details;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mRvContainer = findViewById(R.id.mRvContainer);
        mBtnSubmit = findViewById(R.id.mBtnSubmit);
        mBtnRevise = findViewById(R.id.mBtnRevise);

        mTvWeighbridgeRoute = findViewById(R.id.mTvWeighbridgeRoute);
        mTvContainerNo = findViewById(R.id.mTvContainerNo);
        mTvFlightDate = findViewById(R.id.mTvFlightDate);
        mTvFlightNo = findViewById(R.id.mTvFlightNo);
        mTvWeightingWeight = findViewById(R.id.mTvWeightingWeight);
        mTvOrderWeight = findViewById(R.id.mTvOrderWeight);
        mTvContainerWeight = findViewById(R.id.mTvContainerWeight);
        mTvTotalWeight = findViewById(R.id.mTvTotalWeight);

        mIbBridgeWeigh = findViewById(R.id.mIbBridgeWeigh);
    }

    @Override
    protected void initData() {
        mContainerCode = mBundle.getString(Constant.Container_Code);
        mContainerType = mBundle.getInt(Constant.Container_Type);
        IsEditor = mBundle.getBoolean(Constant.IsEditor);
        ID = mBundle.getInt(Constant.ID);

        ConstantInfo.UnusualIDList.clear();

        CommonUtils.setScreenContainerPaddingTop(mLlContainer);

        mTvTitleName.setText(getString(R.string.title_leave_port_weighing));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);
    }

    @Override
    protected void initConnect() {
        valueParams = new HttpParams();
        valueParams.put(Constant.Container_Code , mContainerCode);
        valueParams.put(Constant.Container_Type , mContainerType);
        valueParams.put(Constant.Key_Is_Done , !IsEditor);
        valueParams.put(Constant.Query_Date , CommonUtils.getDate());
        if(ID != 0)
            valueParams.put(Constant.ID , ID);

        NetworkUtils.getLeavePortWeightingDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mLeavePorWeighingDetailsData = (LeavePorWeighingDetailsData) body;

                mOrderList = mLeavePorWeighingDetailsData.getOrderList();

                initLocationData();

                initChannelData();
            }
        }, valueParams);
    }

    /**
     * 初始化通道数据
     */
    private void initChannelData(){
        if(!TextUtils.isEmpty(mLeavePorWeighingDetailsData.getChannel())){
            mChannelData = new ChannelData(mLeavePorWeighingDetailsData.getChannelId() , mLeavePorWeighingDetailsData.getChannel());
            mTvWeighbridgeRoute.setText(mChannelData.getCode());
        }else {
            NetworkUtils.getCheckChannel(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mChannelDataList = (List<ChannelData>) body;

                    if (mChannelDataList != null && mChannelDataList.size() != 0) {
                        mChannelData = mChannelDataList.get(0);
                        mTvWeighbridgeRoute.setText(mChannelData.getCode());
                    }
                }
            }, Constant.LeavePortDuplicate);
        }
    }

    /**
     * 初始化数据
     */
    private void initLocationData(){
        mBtnSubmit.setVisibility(IsEditor ? View.VISIBLE : View.GONE);
        mBtnRevise.setVisibility(IsEditor ? View.GONE : View.VISIBLE);
        mIbBridgeWeigh.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

        if(IsEditor) {
            mTvWeighbridgeRoute.setOnClickListener(this::onClick);
            mIbBridgeWeigh.setOnClickListener(this::onClick);
            mTvWeightingWeight.setOnClickListener(this::onClick);
        }

        initWeighingWeight(String.valueOf(mLeavePorWeighingDetailsData.getWeight()));// 初始化货物重量
        mTvContainerNo.setText(mLeavePorWeighingDetailsData.getContainerCode());//容器编号
        mTvFlightNo.setText(mLeavePorWeighingDetailsData.getFlightNo());//航班号
        mTvFlightDate.setText(mLeavePorWeighingDetailsData.getFlightDate());//航班日期
        mTvOrderWeight.setText(String.valueOf(mLeavePorWeighingDetailsData.getGoodsWeight()));//货邮重量
        mTvContainerWeight.setText(String.valueOf(mLeavePorWeighingDetailsData.getContainerWeight()));//容器重量
        mTvTotalWeight.setText(String.valueOf(mLeavePorWeighingDetailsData.getGoodsWeight() + mLeavePorWeighingDetailsData.getContainerWeight()));//总重量

        // 初始化列表信息
        initWarehousingAdapter();
    }

    /**
     * 初始化列表
     */
    private void initWarehousingAdapter(){
        mWeighingAdapter = new LeavePorWeighingDetailsAdapter(mActivity, mOrderList);
        mWeighingAdapter.setHasStableIds(true);
        mRvContainer.setAdapter(mWeighingAdapter);
        mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    public void onClick(View v) {
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (v.getId()){
            case R.id.mTvWeighbridgeRoute :
                // 选取通道
                if(mLeavePorWeighingDetailsData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                ChanelDialog.getInstance(mActivity)
                        .getChannelData(Constant.Leave_All)
                        .setOnItemClickListener(new ChanelDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(ChannelData channelData) {
                                mChannelData = channelData ;
                                mTvWeighbridgeRoute.setText(mChannelData.getCode());
                            }
                        }).show();
                break;
            case R.id.mIbBridgeWeigh:
                // 获取重量
                if(mChannelData == null){
                    ToastUtils.showToast(R.string.toast_weighbridge_route_null);
                    return;
                }

                NetworkUtils.getBridgeWeigh(new OkGoBackListener(mActivity) {
                    @Override
                    public void onSuccess(Object body) {
                        initWeighingWeight(body.toString());
                    }
                }, mChannelData.getCode());
                break;
            case R.id.mTvWeightingWeight:
                // 称重重量输入
                double weighingWeight = mLeavePorWeighingDetailsData.getWeight() != 0 ?
                        mLeavePorWeighingDetailsData.getWeight() : mLeavePorWeighingDetailsData.getGoodsWeight() + mLeavePorWeighingDetailsData.getContainerWeight();

                EditorActivity.openActivity(mActivity, new EditorActivity.EditorCallbackListener() {
                    @Override
                    public void onSubmit(String content) {
                        initWeighingWeight(content);
                    }
                } , String.valueOf(weighingWeight));
                break;
            case R.id.mBtnSubmit:
                // 提交
                if(mLeavePorWeighingDetailsData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(mLeavePorWeighingDetailsData.getWeight() == 0){
                    ToastUtils.showToast(R.string.toast_weighting_weight_null);
                    return;
                }

                if(IsSaveIng){
                    ToastUtils.showToast(R.string.toast_save_ing);
                    return;
                }

                AlertDialog.getInstance(mActivity)
                        .setContent(R.string.dialog_confirm_submit_weighing_info)
                        .setConfirmButton(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                clickSubmit();
                            }
                        }).show();
                break;
            case R.id.mBtnRevise:
                // 修改
                if(mLeavePorWeighingDetailsData == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                IsEditor = true ;
                initLocationData();
                break;
            case R.id.mIbBack:
                onBackPressed();
                break;
        }
    }

    private void initWeighingWeight(String content){
        mLeavePorWeighingDetailsData.setWeight(TextUtils.isEmpty(content) ? 0 : Double.parseDouble(content));
        ErrorRouteBean errorRate = CommonUtils.getErrorRoute(mLeavePorWeighingDetailsData.getWeight() , mLeavePorWeighingDetailsData.getContainerWeight(), mLeavePorWeighingDetailsData.getGoodsWeight()) ;

        mTvWeightingWeight.setText(String.valueOf(mLeavePorWeighingDetailsData.getWeight()));
        mTvWeightingWeight.setTextColor(getColor(errorRate.isError() ? R.color.color_red : R.color.text_color));
    }

    /**
     * 提交数据
     */
    private void clickSubmit() {
        IsSaveIng = true ;

        NetworkUtils.saveLeavePortWeightingDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                IsSaveIng = false;
                ToastUtils.showToast(R.string.toast_submit_success);
                onFinish(Constant.RESULT_CODE);
            }

            @Override
            public void onError() {
                IsSaveIng = false;
            }
        }, JsonUtils.parseBeanToJson(mLeavePorWeighingDetailsData));
    }
}
